﻿using System.Collections.Generic;
using System.Web;

namespace Jayden.Dll.BitTorrent
{
	public class TrackerHttpRequest
	{

		public string CreateHttpRequest(
			string announceUrl,
			byte[] infoHash,
			byte[] peerId,
			int port,
			long uploaded,
			long downloaded,
			long left,
			bool compact,
			bool noPeerId,
			string eventName,
			int? numwant,
			string key,
			string trackerId)
		{
			List<string> query = new List<string>();
			query.Add(string.Format("info_hash={0}", HttpUtility.UrlEncode(infoHash)));
			query.Add(string.Format("peer_id={0}", HttpUtility.UrlEncode(peerId)));
			query.Add(string.Format("port={0}", port));
			query.Add(string.Format("uploaded={0}", uploaded));
			query.Add(string.Format("downloaded={0}", downloaded));
			query.Add(string.Format("left={0}", left));
			query.Add(string.Format("compact={0}", compact ? "1" : "0"));
			if(!compact)
				query.Add(string.Format("no_peer_id={0}", noPeerId ? "1" : "0"));
			if(!string.IsNullOrWhiteSpace(eventName))
				query.Add(string.Format("event={0}", eventName));
			if(numwant.HasValue)
				query.Add(string.Format("numwant={0}", numwant.Value));
			if(!string.IsNullOrWhiteSpace(key))
				query.Add(string.Format("key={0}", key));
			if(!string.IsNullOrWhiteSpace(trackerId))
				query.Add(string.Format("trackerid={0}", trackerId));
			return announceUrl + "?" + string.Join("&", query);
		}


	}
}
